package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.ApplyImageAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.PublishDetailBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.OnRecyclerViewItemClickListener;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.ShareDialog;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PublishDetailActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_adress)
    TextView tvAdress;
    @BindView(R.id.tv_label)
    TextView tvLabel;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.et_content)
    TextView etContent;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.rl_look)
    RelativeLayout rlLook;
    @BindView(R.id.title_right_two_btn)
    ImageView titleRightTwoBtn;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.title_right_one_tv2)
    TextView titleRightOneTv2;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.title_right_one_btn)
    ImageView titleRightOneBtn;
    @BindView(R.id.tv_treatment)
    TextView tvTreatment;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    @BindView(R.id.tv_attention)
    TextView tvAttention;
    @BindView(R.id.tv_more)
    TextView tvMore;
    private String workId;
    private ApplyImageAdapter mAdapter;
    private boolean is_me = false;
    private String shareUrl, qrCode;
    public String lat, longi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_detail);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        is_me = getIntent().getBooleanExtra(Constant.IS_ME, false);

        if (is_me) {
            rlLook.setVisibility(View.VISIBLE);
            tvConfirm.setText("查看报名情况");
        }
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("发布详情");
        titleRightOneBtn.setVisibility(View.VISIBLE);
        titleRightOneBtn.setImageResource(R.mipmap.more);
        workId = getIntent().getStringExtra("id");
        getDatas();
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.WorkDesc, this)
                .params("workid", workId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        PublishDetailBean parentBean = DealResult.getInstance().dealDatas(PublishDetailActivity.this, response, new TypeToken<PublishDetailBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS || parentBean.getCode() == Constant.EMPTY) {
                            setDatas(parentBean.getBody());
                        } else {
                            ToastUtils.toastForShort(PublishDetailActivity.this, parentBean.getResult());
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(PublishDetailActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(PublishDetailBean.BodyBean body) {
        if (body == null) {
            return;
        }
        tvAttention.setText(body.getPromptText());
        tvTitle.setText(body.getWorktitle());
        tvAdress.setText(body.getWorkarea());
        tvLabel.setText(body.getLabelStr());
        tvNum.setText(body.getNumber() + "");

        longi = body.getLongitude();
        lat = body.getLat();
        shareUrl = body.getShareurl();
        qrCode = body.getQrcode();
        if (body.getTimetype() == 1) {
            tvTreatment.setText("每天" + body.getPrice() + "元");
            tvTime.setText(body.getYxtime() + "天");
        } else {
            tvTime.setText(body.getYxtime() + "天");
            tvTreatment.setText("每小时" + body.getPrice() + "元");
        }

        switch (body.getWorksex()) {
            case 1:
                tvSex.setText("男");
                break;
            case 2:
                tvSex.setText("女");
                break;
            case 3:
                tvSex.setText("不限");
                break;
        }
        etContent.setText(body.getWorkdesc());
        int me=body.getIsme();
        if(me==1){
            tvConfirm.setText("查看报名情况");
        }else{
            switch (body.getIsbm()) {
                case 1:
                    tvConfirm.setOnClickListener(null);
                    tvConfirm.setText("已报名");
                    break;
                case 2:
                    tvConfirm.setText("去报名");
                    break;
            }
        }
//        if (!is_me) {
//            switch (body.getIsbm()) {
//                case 1:
//                    tvConfirm.setOnClickListener(null);
//                    tvConfirm.setText("已报名");
//                    break;
//                case 2:
//                    tvConfirm.setText("去报名");
//                    break;
//            }
//        }else{
//            tvConfirm.setText("查看报名情况");
//        }
        List<PublishDetailBean.BodyBean.BmqkBean> images = body.getBmqk();
        if (!images.isEmpty()) {
            tvMore.setVisibility(View.VISIBLE);
            LinearLayoutManager manager = new LinearLayoutManager(this);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(manager);
            mAdapter = new ApplyImageAdapter(PublishDetailActivity.this, images);
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, Object data) {
                    Intent i = new Intent(PublishDetailActivity.this, ApplyListActivity.class);
                    i.putExtra("id", workId);
                    startActivity(i);
                }
            });
        } else {
            tvMore.setVisibility(View.INVISIBLE);
        }
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm, R.id.rl_look, R.id.title_right_one_btn, R.id.ll_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_btn:
                doShare();
                break;
            case R.id.tv_confirm:
                String confirm=tvConfirm.getText().toString();
                if(TextUtils.equals(confirm,"查看报名情况")||TextUtils.equals(confirm,"已报名")){
                    Intent i = new Intent(PublishDetailActivity.this, ApplyListActivity.class);
                    i.putExtra("id", workId);
                    startActivity(i);
                }else {
                    confirm();
                }

                break;
            case R.id.rl_look:
                Intent i = new Intent(this, ApplyListActivity.class);
                i.putExtra("id", workId);
                startActivity(i);
                break;
            case R.id.ll_address:
//                Intent intent=new Intent(this, MapActivity.class);
//                intent.putExtra("lon",longi);
//                intent.putExtra("lat",lat);
//                startActivity(intent);
                break;
        }
    }

    private void confirm() {
        loadingDialog.show();
        OkgoUtil.post(Http.SignUp, this)
                .params("workid", workId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(PublishDetailActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            getDatas();
                        } else {
                        }
                        ToastUtils.toastForShort(PublishDetailActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(PublishDetailActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void doShare() {
        View contentView = LayoutInflater.from(PublishDetailActivity.this).inflate(R.layout.popup_more, null);
        TextView tvRemark = contentView.findViewById(R.id.tv_remark);
        TextView tvMoney = contentView.findViewById(R.id.tv_money);
        TextView tvShare = contentView.findViewById(R.id.tv_share);
        TextView tvAttention = contentView.findViewById(R.id.tv_attention);
        tvRemark.setVisibility(View.GONE);
        tvAttention.setVisibility(View.GONE);
        final PopupWindow popWnd = new PopupWindow(PublishDetailActivity.this);
        popWnd.setContentView(contentView);
        popWnd.setBackgroundDrawable(null);
        popWnd.setWidth(380);
        popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popWnd.setOutsideTouchable(true);
        if (!popWnd.isShowing()) {
            popWnd.showAsDropDown(titleRightOneBtn, -360, 20);
        }
        tvMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWnd.dismiss();
                ShareDialog dialog = new ShareDialog(PublishDetailActivity.this, shareUrl);
                dialog.show();
            }
        });
        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWnd.dismiss();
                ShareDialog dialog = new ShareDialog(PublishDetailActivity.this, qrCode);
                dialog.show();

            }
        });
    }
}
